package comment

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"html"

	"com.jiaguyu/model"
	"com.jiaguyu/until"

	"com.jiaguyu/middleware/account"

	"com.jiaguyu/logger"

	"com.jiaguyu/id_gen"

	"com.jiaguyu/dao/db"
)

const (
	MinCommentContentSize = 10
)

func PostCommentHandle(ctx *gin.Context) {
	var comment model.Comment

	err := ctx.BindJSON(&comment)

	if err != nil {
		fmt.Printf("BindJSON参数问题%v\n", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}

	//日志输入的地方
	fmt.Printf("日志输入参数%v\n", comment)

	if len(comment.Content) <= MinCommentContentSize {
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}
	userId, err := account.GetUserId(ctx)

	if err != nil || userId == 0 {
		until.ResponseError(ctx, until.ErrCodeNotLogin)
		return
	}
	//本来运用本地的userid 这里做个测试 闯入的id
	//comment.AuthorId = userId

	//转义content
	comment.Content = html.EscapeString(comment.Content)
	//2 生成评论 id
	cid, err := id_gen.GetId()
	if err != nil {
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		logger.Error("生成数据问题%V:err", err)
		return
	}
	comment.CommentId = int64(cid)
	err = db.CreatePostComment(&comment)
	if err != nil {
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		logger.Error("生成数据问题%V:err", err)
		return
	}

	until.ResponseSuccess(ctx, nil)
}

//回复列表
func PostCommentReplyHandle(ctx *gin.Context) {
	var comment model.Comment

	err := ctx.BindJSON(&comment)

	if err != nil {
		logger.Error("参数问题%V:err", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}

	//日志输入的地方
	fmt.Printf("参数问题%v\n", comment)

	//参数问题
	if len(comment.Content) <= MinCommentContentSize || comment.QuestionId == 0 || comment.ReplyCommentId == 0 || comment.ParentId == 0 {
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}

	userId, err := account.GetUserId(ctx)

	if err != nil || userId == 0 {
		until.ResponseError(ctx, until.ErrCodeNotLogin)
		return
	}

	comment.AuthorId = userId

	//转义content
	comment.Content = html.EscapeString(comment.Content)
	//2 生成评论 id
	cid, err := id_gen.GetId()
	if err != nil {
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		logger.Error("生成数据问题%V:err", err)
		return
	}
	//根据回复 replycommentid  查询这个replycommentid的作者是谁
	comment.CommentId = int64(cid)
	err = db.CreateReplyComment(&comment)
	if err != nil {
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		logger.Error("生成数据问题%V:err", err)
		return
	}

	until.ResponseSuccess(ctx, nil)
}
func CommentListHandle(ctx *gin.Context) {

	answerId, err := until.GetQueryInt64(ctx, "answer_id")
	offset, err := until.GetQueryInt64(ctx, "offset")
	limit, err := until.GetQueryInt64(ctx, "limit")

	if err != nil {
		logger.Error("获取参数answerID,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}
	logger.Debug("get query answer_id success id:%v", answerId)

	//answer_id 拿到手
	commentList, count, err := db.GetCommentList(answerId, offset, limit, 1)
	if err != nil {
		logger.Error("获取commentList,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	var userIdList []int64

	for _, v := range commentList {
		userIdList = append(userIdList, v.AuthorId)
	}
	userInfoList, err := db.GetUserInfoList(userIdList)
	if err != nil {
		logger.Error("获取userInfoList,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	userInfoMap := make(map[int64]*model.UserInfo, len(userIdList))

	for _, user := range userInfoList {
		userInfoMap[user.UserId] = user
	}

	//替换名称we

	for _, commentitem := range commentList {
		user, ok := userInfoMap[commentitem.AuthorId]
		if ok {
			commentitem.AuthorName = user.UserName
		}
		user, ok = userInfoMap[commentitem.ReplyAuthorId]
		if ok {
			commentitem.ReplyAuthorName = user.UserName
		}
	}

	var apiCommentList = &model.CommentList{}
	apiCommentList.CommentList = commentList
	apiCommentList.Count = count

	until.ResponseSuccess(ctx, apiCommentList)
}

//回复列表接口
func ReplyListHandle(ctx *gin.Context) {

	commentId, err := until.GetQueryInt64(ctx, "comment_id")
	offset, err := until.GetQueryInt64(ctx, "offset")
	limit, err := until.GetQueryInt64(ctx, "limit")

	if err != nil {
		logger.Error("获取参数answerID,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}
	logger.Debug("get query commentId success id:%v", commentId)

	//commentId 拿到手
	replyCommentList, count, err := db.GetReplyCommentList(commentId, offset, limit, 2)
	if err != nil {
		logger.Error("获取replyCommentList,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	var userIdList []int64

	for _, v := range replyCommentList {
		userIdList = append(userIdList, v.AuthorId)
	}
	userInfoList, err := db.GetUserInfoList(userIdList)
	if err != nil {
		logger.Error("获取userInfoList,err:%v", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	userInfoMap := make(map[int64]*model.UserInfo, len(userIdList))

	for _, user := range userInfoList {
		userInfoMap[user.UserId] = user
	}

	//替换名称we

	for _, replycomment := range replyCommentList {
		user, ok := userInfoMap[replycomment.AuthorId]
		if ok {
			replycomment.AuthorName = user.UserName
		}
		user, ok = userInfoMap[replycomment.ReplyAuthorId]
		if ok {
			replycomment.ReplyAuthorName = user.UserName
		}
	}

	var apiReplyCommentList = &model.CommentList{}
	apiReplyCommentList.CommentList = replyCommentList
	apiReplyCommentList.Count = count

	until.ResponseSuccess(ctx, apiReplyCommentList)
}

func CommentLikeHandle(ctx *gin.Context) {
	var like model.Like

	err := ctx.BindJSON(&like)
	if err != nil {
		logger.Error("BindJSON参数问题%v\n", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}

	if like.LikeId == 0 || (like.LikeType != model.LikeTypeAnswer && like.LikeType != model.LikeTypeComment) {
		logger.Error("参数问题%v\n", err)
		until.ResponseError(ctx, until.ErrCodeParameter)
		return
	}
	if like.LikeType == model.LikeTypeAnswer {
		err = db.UpdateAnswerCount(like.LikeId)
	} else {
		err = db.UpdateCommentCount(like.LikeId)
	}
	if err != nil {
		logger.Error("插入点赞失败%v\n", err)
		until.ResponseError(ctx, until.ErrCodeServerBusy)
		return
	}
	until.ResponseSuccess(ctx, nil)
}
